package com.twotiger.and.activity.others;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.util.LogUtil;
import com.twotiger.and.util.PromptManager;
import com.twotiger.and.util.StringUtils;
import com.twotiger.and.util.ViewUtils;

public class FeedBackPage extends BaseActivity implements OnClickListener {

    private String TAG = "FeedBackPage";
    private Button btnSubmit;
    private EditText feedbackEmail;
    private EditText feedbackProblem;
    private TextView hint_num_tv;
    private int requestCode = 20010;
    private HashMap<String, String> params;
    private final int FEEDBACKSUCCES = 0;
    private final int FEEDBACKFAIL = 1;
    private View sub_title_left_ll;
    private TextView sub_title_center_tv;
    private ImageView sub_title_left_bt;

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.feed_back_layout, null);
        return view;
    }

    @Override
    protected void findViewById(View view) {
        btnSubmit = (Button) view.findViewById(R.id.btnSubmit);
        feedbackEmail = (EditText) view.findViewById(R.id.feedbackEmail);
        feedbackProblem = (EditText) view.findViewById(R.id.feedbackProblem);
        hint_num_tv = (TextView) view.findViewById(R.id.hint_num_tv);
        sub_title_left_ll = view.findViewById(R.id.sub_title_left_ll);
        sub_title_center_tv = (TextView) view.findViewById(R.id.sub_title_center_tv);
        sub_title_left_bt = (ImageView) view.findViewById(R.id.sub_title_left_bt);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.sub_title_left_ll:
            hideSoftInputMethod(FeedBackPage.this, view);
            onBackPress();
            break;
        case R.id.btnSubmit:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            String email = feedbackEmail.getText().toString().trim();
            String feedback = feedbackProblem.getText().toString().trim();
            if (StringUtils.isEmpty(email)) {
                PromptManager.showCustomToast(FeedBackPage.this, "请留下您的联系方式");
                return;
            }
            params.clear();
            params.put("token", getToken());
            params.put("contact", email);
            try {
                params.put("content", URLEncoder.encode(feedback, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            getDate(params, Constants.FEEDBACK, handler, FEEDBACKSUCCES, FEEDBACKFAIL, true, true,
                false);
            break;

        }
    }

    @Override
    protected void setListener() {
        handler = new BaseHandler(this) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case FEEDBACKSUCCES:
                    //	                AlertDialog alertDialog = PromptManager.showSimpleDialog(getActivity(), "留言成功", "感谢您对两只老虎的大力支持", "确定");
                    //	                alertDialog.show();
                    Basebean result = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if(result.isOk()){
                        showToast("留言成功，感谢您的支持");
                        onBackPress();
                    }
                    break;
                case FEEDBACKFAIL:
                    PromptManager.showCustomToast(FeedBackPage.this, "留言失败");
                    break;
                default:
                    break;
                }

                super.handleMessage(msg);
            }
        };
        sub_title_left_ll.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
        feedbackProblem.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String feedback = feedbackProblem.getText().toString();
                if (feedback != null & feedback.length() <= 50) {
                    hint_num_tv.setText(feedback.length() + "/50");
                } else {

                }
            }
        });
    }

    @Override
    protected void processLogic() {
        params = getParm();
        initPage();
    }

    private void initPage() {
        // 初始化标题
        sub_title_center_tv.setText("意见反馈");
        sub_title_left_bt.setVisibility(View.VISIBLE);
        try {
        } catch (Exception e) {
            LogUtil.info(e.getMessage() + "");
        }
    }

    @Override
    protected void onBackPress() {
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }
}
